package satir.framework.graph;

import javax.swing.JFrame;
import java.awt.Dimension;
import java.util.ArrayList;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.SpiderWebPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.*;

public class Generator extends JFrame {
	private static final long serialVersionUID = 1L;  
	private ArrayList<Person> personList;
	private String chartTitle;
	
	
	public Generator(String applicationTitle, String chartTitle)
    {
			super(applicationTitle);
			this.chartTitle = chartTitle;
            personList = new ArrayList<Person>();
    }

    private CategoryDataset createDataset()
    {  
    	Person player;
    	ArrayList<Data> dataList;
    	DefaultCategoryDataset defaultcategorydataset = new DefaultCategoryDataset();
    	System.out.println(personList.size());
    	for(int i=0; i < personList.size(); i++) {
    		player = personList.get(i);
    		dataList = player.getDataList();
    		for(int j=0; j < dataList.size(); j++) {
    			defaultcategorydataset.addValue(dataList.get(j).getValue(), player.getName(), dataList.get(j).getLabel());
    		}
    	}
    	return defaultcategorydataset;
    }

    private JFreeChart createChart(CategoryDataset categorydataset)
    {
            SpiderWebPlot spiderwebplot = new SpiderWebPlot(categorydataset);
            spiderwebplot.setStartAngle(54D);
            spiderwebplot.setInteriorGap(0.40000000000000002D);
            spiderwebplot.setToolTipGenerator(new StandardCategoryToolTipGenerator());
            JFreeChart jfreechart = new JFreeChart(chartTitle, TextTitle.DEFAULT_FONT, spiderwebplot, false);
            LegendTitle legendtitle = new LegendTitle(spiderwebplot);
            legendtitle.setPosition(RectangleEdge.BOTTOM);
            jfreechart.addSubtitle(legendtitle);
            return jfreechart;
    }

    public JPanel createDemoPanel()
    {
    	JFreeChart jfreechart = createChart(createDataset());
    	return new ChartPanel(jfreechart);
    }
    
    public void addPerson(Person player) {
    	personList.add(player);
    }
    
    public void setCharTitle(String chartTitle) {
    	this.chartTitle = chartTitle;
    }
    public void generate() {
    	JPanel jpanel = createDemoPanel();
        jpanel.setPreferredSize(new Dimension(500, 500));
        setContentPane(jpanel);
    }
}
